package cn.bonoon.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.bonoon.core.plugins.FaqService;
import cn.bonoon.core.plugins.ServerService;
import cn.bonoon.entities.ServerEntity;
import cn.bonoon.entities.plugins.FaqEntity;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.models.DialogModel;
import cn.bonoon.util.ServerUtil;

@Controller
@RequestMapping("s/sys")
public class PartEmbedController extends AbstractController {
	@Autowired
	private ServerService serverService;

	@Autowired
	private FaqService faqService;

	private String imgWeb, imgApplication, imgConnector, imgMQ;

	@Override
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
		imgWeb = toUrl("/res/images/web-server.png");
		imgApplication = toUrl("/res/images/server.png");
		imgConnector = toUrl("/res/images/connecter.png");
		imgMQ = toUrl("/res/images/server.png");
	}

	@RequestMapping("{key}!faq")
	public String faq(Model model, @PathVariable("key") String key) {
		List<FaqEntity> faqs = faqService.faqs(key, getUser());
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (FaqEntity entity : faqs) {
			sb.append("<tr><td style='padding-top:10px;'><font style='line-height:150%;font-weight:bold;'>").append(++i).append('.')
					.append(entity.getQuestion()).append("</font></td></tr><tr><td style='padding-bottom:10px;'><font style='line-height:150%;'>")
					.append(entity.getAnswer()).append("</font></td></tr>");
		}
		model.addAttribute("content", sb);
		return "sys/sys-faq";
	}

	@RequestMapping("server")
	public String server(Model model) {
		LogonUser user = getUser();
		String _title = "<font style='display:inline-block;width:80px;'>";
		if (user.isSuper()) {
			StringBuilder servers = new StringBuilder();
			for (ServerEntity se : serverService.activities()) {
				servers.append("<tr><td style='width:65px;height:65px;'><img src='");
				switch (se.getType()) {
				case ServerService.TYPE_WEB:
					servers.append(imgWeb).append("' title='web服务器");
					break;
				case ServerService.TYPE_APPLCATION:
					servers.append(imgApplication).append("' title='应用服务器");
					break;
				case ServerService.TYPE_CONNECTER:
					servers.append(imgConnector).append("' title='连接服务器");
					break;
				case ServerService.TYPE_MQ:
					servers.append(imgMQ).append("' title='消息队列服务器");
					break;
				default:
					servers.append(imgWeb).append("' title='web服务器");
					break;
				}
				servers.append("'/></td><td style='padding:10px 0 0 10px;line-height:180%;' valign='top'><div>").append(_title).append("IP</font>： ").append(se.getIps());
				servers.append("</div><div>").append(_title).append("启动时间</font>： ").append(StringHelper.datetime2String(se.getStartAt()));
				servers.append("</div><div>").append(_title).append("更新时间</font>： ").append(StringHelper.datetime2String(se.getRefreshAt()));
				servers.append("</div><div>").append(_title).append("操作系统</font>： ").append(se.getOsInfo());
				servers.append("</div><div>").append(_title).append("JVM</font>： ").append(se.getJvmInfo());
				servers.append("</div><div>").append(_title).append("服务器</font>： ").append(se.getServerInfo());
				servers.append("</div><br/><div>硬件信息</div><div>").append(_title).append("CPU</font>： ").append(se.getCpuInfo());
				servers.append("</div><div>").append(_title).append("内存</font>： ").append(se.getMemoryInfo());
				servers.append("</div><div>").append(_title).append("硬盘</font>： ").append(se.getHardDiskInfo());
				servers.append("</div><div>").append(_title).append("其它硬件信息</font>： ").append(se.getOtherHardwareInfo());
				servers.append("</div></td></tr>");
				String server = ServerUtil.getServerId();
				System.out.println("-->" + server);
				System.out.println("-->" + ServerUtil.isJetty());
			}
			model.addAttribute("servers", servers);
		}
		return "/sys/sys-server";
	}

	@RequestMapping("timeline")
	public String timeline(Model model) {
		return "sys/timeline";
	}
	
	@RequestMapping(value = "timeline_img_dlg", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView timeline_img_dlg(HttpServletRequest request, String src) {
		DialogModel model = new DialogModel(request);
		model.addObject("src", src);
		return model.execute("sys/timeline_img_dlg");
	}
}
